<?php
/**
 * IndexController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class LinkController extends Thaind_Controller_Action
{
	var $_arrParam;
    public function init()
    {

//    	Truyá»�n tham sá»‘ ra view

    	$this->_arrParam = $this->_request->getParams();
    	if (!isset($this->_arrParam['page'])){
    		$this->_arrParam['page'] = 1;
    	}
    	if (!isset($this->_arrParam['line_per_page'])){
    		$this->_arrParam['line_per_page'] = 10;
    	}
    	$this->view->arrParam = $this->_arrParam;
    }

    public function preDispatch(){
    	$auth = Thaind_Auth_Admin::getInstance();
    	if (!$auth->hasIdentity()) {
    		$this->_arrParam['action'] = 'login';
			$this->_forward('login','index','default',$this->_arrParam);
    	}
		$template_path = TEMPLATE_PATH . "/default/default";
		$this->loadTemplate($template_path,'template.ini','template');
    	
    }
    
    public function indexAction(){
    	
//    	Load model sử dụng trong action
		
    	Zend_Loader::loadClass("Default_Models_Mongo_LinkModel");
    	
//    	Khai báo biến

    	$link_model = new Default_Models_Mongo_LinkModel();
    	$link_list = $link_model->getAllLink();
    	
		$adapter = new Shanty_Paginator_Adapter_Mongo($link_list);
    	$this->setPagination($adapter, $this->_arrParam);
    
    }
    public function addAction(){

//    	Load model sử dụng trong action

    	Zend_Loader::loadClass("Default_Models_Mongo_LinkModel");
    	Zend_Loader::loadClass("Default_Forms_LinkForm");
    	
//    	Khai báo biến

    	$link_model = new Default_Models_Mongo_LinkModel();
    	$form = new Default_Forms_LinkForm(array("submit"			=> "Thêm",
    												"cancel_link"	=> "/link",
    	));
    	
//    	Nếu user submit form thì insert dữ liệu

    	if ($this->_request->isPost()){
    		if ($form->isValid($this->_arrParam)){
    			$data =$form->getValues();
    			$data['created_time'] = new MongoDate();
    			$data['status'] = true;
    			$link_model->insert($data);
    			$this->_redirect("/link");
    		}
    	}
    	
//    	Truyền biến ra view

    	$this->view->form = $form;
    }
    public function editAction(){

//    	Load model sử dụng trong action

    	Zend_Loader::loadClass("Default_Models_Mongo_LinkModel");
    	Zend_Loader::loadClass("Default_Forms_LinkForm");
    	
//    	Khai báo biến

    	$link_model = new Default_Models_Mongo_LinkModel();
    	$form = new Default_Forms_LinkForm(array("submit"			=> "Thêm",
    												"cancel_link"	=> "/link",
    	));
    	
//    	Nếu user submit form thì insert dữ liệu

    	if ($this->_request->isPost()){
    		if ($form->isValid($this->_arrParam)){
    			$data = array("\$set"	=> $form->getValues());
    			$where = array('_id'	=> new MongoId($this->_arrParam['id']));
    			$link_model->update($where,$data);
    			$this->_redirect("/link");
    		}
    	}else{
    		$link_info = $link_model->find($this->_arrParam['id']);    	
    		if ($link_info){
    			$form->setDefaults($link_info->export());
    		}
    	}
    	
//    	Truyền biến ra view

    	$this->view->form = $form;
    	$this->render("add");
    }
}
